﻿@model  ArticleListModel
@{
    Layout = "~/Administrator/Views/Shared/_AdminLayout.cshtml";
    var random = CAF.Infrastructure.Core.Utilities.CommonHelper.GenerateRandomInteger();
    var clientId = "article" + random;

    var gridPageSize = EngineContext.Current.Resolve<CAF.Infrastructure.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    ViewBag.Title = T("Admin.ContentManagement.Articles").Text;
    var g = Html.GridToolKit<ArticleModel>();
    var grid = g.DataGrid("articleGrid", g.Param(x => x.Id)).TableId("table-id").GridKey("Id").PageLength(gridPageSize)
    .MainGrid(
        //g.GridColumn(x => x.PictureThumbnailUrl, 50).DefaultContent("<img alt='#Id#' src='#PictureThumbnailUrl#'  class='zoomable-thumb' />").Hidden(Model.DisplayArticlePictures),
    g.GridColumn(null, null, 30).DefaultContent("<input type='checkbox' class='checkboxes' value='#Id#'>"),
    g.GridColumn(x => x.Title, 200).DefaultContent("<a href='" + @Url.Content("~/Admin/Article/Edit/") + "#Id#' >#Title# <i class='fa fa-edit'></i></a>"),
    g.GridColumn(x => x.Comments, 50).DefaultContent("<a href=\"Comments?filterByArticleId=#Id#\"><span class=\"badge badge-success\">#Comments#</span></a>"),
        //g.GridColumn(x => x.IsPasswordProtected, 50),
    g.GridColumn(x => x.DisplayOrder, 50).DefaultContent("<input id=\"no_order_#Id#\" type=\"text\" class=\"form-control\" value=\"#DisplayOrder#\">"),
    g.GridColumn(x => x.Click, 50).DefaultContent("<span class=\"badge badge-danger\">#Click#</span>"),
    g.GridColumn(x => x.IsTop, 50),
    g.GridColumn(x => x.IsRed, 50),
    g.GridColumn(x => x.IsHot, 50),
    g.GridColumn(x => x.IsSlide, 50),
    g.GridColumn(x => x.AllowComments, 50),
    g.GridColumn(x => x.StatusName, 50).DefaultContent("<span class=\"label label-primary\">#StatusName#</span>")
        //g.GridColumn(x => x.DownloadId, 50)
    )
    .Url(Url.Action("List", "Article", new { SearchCategoryId = Model.SearchCategoryId })).DefaultTableClass().BindDataParamBinding("onDataBinding");

    Html.AddScriptParts(ResourceLocation.Head, "~/Content/assets/global/plugins/jstree/dist/jstree.js");
    Html.AddCssFileParts("~/Content/assets/global/plugins/jstree/dist/themes/default/style.css");
}
<script type="text/javascript">
    function onDataBinding() {
        var userRoleIds = [];

        var searchModel = {
            SearchArticleName: $('#@Html.FieldIdFor(model => model.SearchArticleName)').val(),
            SearchCategoryId: $('#CategoriesId').val(),
            SearchSiteId: $('#SearchSiteId').val(),
            SearchWithoutCategories: $('#SearchWithoutCategories').is(':checked')
        };
        return searchModel;
    }

    var categoriesData = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.CategoriesObject));
    $(document).ready(function () {
        @(grid)
        //"delete selected" button
        $('#delete-selected').click(function (e) {
            bootbox.confirm("你确定删除?", function (result) {
                if (result) {
                    e.preventDefault();
                    selectedIds = articleGrid.getSelectedRows();
                    var postData = {
                        selectedIds: selectedIds
                    };
                    $.ajax({
                        cache: false,
                        type: "POST",
                        url: "@(Url.Action("DeleteSelected", "Article"))",
                        data: postData,
                        complete: function (data) {
                            //reload grid
                            articleGrid.reload();
                            selectedIds = [];
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(thrownError);
                        },
                        traditional: true
                    });
                }
            });
            return false;
        });
        // show/hide category filter
        $('input[id="@Html.FieldIdFor(model => model.SearchWithoutCategories)"]').on('switchChange.bootstrapSwitch', function (event, state) {
            $('#SearchCategoryIdContainer').toggle(!state);
        });
        //search button
        $('#btnSearch').click(function () {
            //search
            articleGrid.reload();
            return false;
        });

        //$('#jstreeCategories')
        //  .on('changed.jstree', function (e, data) {
        //      var obj= data.instance.get_node(data.selected[0]);
        //      $('#CategoriesId').val(obj.id);
        //      articleGrid.reload();
        //  }).jstree({
        //      // 'plugins': ["checkbox"],
        //      //'checkbox': { cascade: "", three_state: false },
        //      'core': {
        //          'data': categoriesData,
        //          "themes": {
        //              "responsive": false
        //          }
        //      }
        //  }).bind('loaded.jstree', null);

        $("#bulkEditSave").click(function (e) {
            e.preventDefault();

            var selectedIds = articleGrid.getSelectedRows();
            if(selectedIds.length <= 0)
            {
                bootbox.alert("至少选择选择一项内容!");
                return;
            }

            var postData = {
                selectedIds: selectedIds,
                CategoryId: $("#CategoryId").val(),
                TemplateId: $("#TemplateId").val(),
                AllowComments:$("#@Html.FieldIdFor(model => model.BatchCategory.AllowComments)").is(":checked"),
                IsTop:$("#@Html.FieldIdFor(model => model.BatchCategory.IsTop)").is(":checked"),
                IsRed:$("#@Html.FieldIdFor(model => model.BatchCategory.IsRed)").is(":checked"),
                IsHot:$("#@Html.FieldIdFor(model => model.BatchCategory.IsHot)").is(":checked"),
                IsSlide:$("#@Html.FieldIdFor(model => model.BatchCategory.IsSlide)").is(":checked"),
                OpenCategorieCheckBox:$("#@Html.FieldIdFor(model => model.BatchCategory.OpenCategorieCheckBox)").is(":checked"),
                OpenTemplateCheckBox:$("#@Html.FieldIdFor(model => model.BatchCategory.OpenTemplateCheckBox)").is(":checked"),
                OpenCheckBox:$("#@Html.FieldIdFor(model => model.BatchCategory.OpenCheckBox)").is(":checked"),
            };
            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.Action("BulkEditSave", "Article"))",
                data: postData,
                complete: function (data) {
                    $('#bulkedit-window').modal('toggle');

                    articleGrid.reload();
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError);
                },
                traditional: true
            });

        });
        $("#bulkeditOrder").click(function (e) {
            e.preventDefault();

            var selectedIds = articleGrid.getSelectedRows();
            if(selectedIds.length <= 0)
            {
                bootbox.alert("至少选择选择一项内容!");
                return;
            }
            var updatedArticles =[];
            $.each(selectedIds, function(i, value){
                var idv=$("#no_order_"+value+"").val();
                if(idv !="")
                {
                    updatedArticles.push(value+"|"+idv);
                }
            });

            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.Action("BulkEditOrderSave", "Article"))",
                data: {updatedArticles:updatedArticles},
                complete: function (data) {

                    articleGrid.reload();
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError);
                },
                traditional: true
            });

        });
    });
</script>
<div class="row">

    <div class="col-md-12">
        <div class="portlet light">
            <div class="section-header sticky">
                <div class="caption">
                    <i class="icon-equalizer font-red-sunglo"></i>
                    <span class="caption-subject font-red-sunglo bold uppercase">@T("Admin.ContentManagement.Articles")</span>
                    <span class="caption-helper"></span>
                </div>
                <div class="tools">
                </div>
                <div class="actions">
                    <a href="@Url.Action("Create", new { categoryId =Model.SearchCategoryId})" class="btn yellow"><i class="fa fa-plus"></i>@T("Admin.Common.AddNew") </a>
                    <button type="submit" id="delete-selected" class="btn green">
                        <i class="fa fa-trash-o"></i>
                        @T("Admin.Common.Delete.Selected")
                    </button>
                    <a href="#bulkedit-window" data-toggle="modal" class="btn btn-default">
                        <i class="fa fa-arrow-left"></i>&nbsp;批量修改
                    </a>
                    <a href="javascript:void(0);" id="bulkeditOrder" class="btn btn-primary">
                        <i class="fa fa-arrow-left"></i>&nbsp;保存排序
                    </a>
                </div>
            </div>
            <div class="portlet-body form form-horizontal form-bordered form-row-stripped">
                <div class="form-body">
                    <div class="row">
                        <div class="col-md-6">

                            <div class="input-group">
                                <div class="input-icon">
                                    <label class="control-label col-md-4">@Html.LangLabelFor(model => model.SearchArticleName)</label>
                                    <div class="col-md-8">
                                        @Html.EditorFor(model => model.SearchArticleName)

                                    </div>
                                </div>
                                <span class="input-group-btn">
                                    <button type="button" id="btnSearch" class="btn btn-default">
                                        <i class="fa fa-search"></i>
                                        @T("Admin.Common.Search")
                                    </button>
                                </span>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="portlet-body flip-scroll">
                <table id="@grid.GetTableId" class="@grid.GetDefaultTableClass" @Html.Raw(grid.GetFixedLayout ? "style='table-layout:fixed'" : "")>
                    <thead>
                        <tr>
                            @foreach (var column in grid.GetColumns)
                            {
                                if (column.Name == "" + T("Admin.Common.Edit").Text + "")
                                {

                                    <th class="@column.CssClassHeader">@column.DisplayName</th>

                                }
                                else if (column.DisplayName == null)
                                {

                                    <th class="@column.CssClassHeader"><input type="checkbox" class="group-checkable" data-set="checkboxes"></th>

                                }
                                else
                                {
                                    <th class="@column.CssClassHeader">@column.DisplayName</th>
                                }
                            }
                        </tr>

                    </thead>
                    <tbody></tbody>
                </table>
            </div>
        </div>
    </div>
</div>

@{Html.CafSite().Window()
    .Name("bulkedit-window")
    .Title(T("Admin.ContentManagement.BulkEdit").Text)
    .Content(@<text>
        @using (Html.BeginForm())
        {
            <input type="hidden" id="SelectedIds" name="SelectedIds" />
            <div class="portlet-body form form-horizontal">
                <div class="form-body">

                    <div class="form-group">
                        <label class="control-label col-md-3">   @T("Admin.ContentManagement.ArticleCategory").Text</label>
                        <div class="col-md-9">
                            <div class="input-group">
                                <span class="input-group-addon">
                                    @Html.CheckBoxFor(model => model.BatchCategory.OpenCategorieCheckBox)
                                </span>
                                @Html.DropDownList("CategoryId", Model.AvailableCategories, "", new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.AvailableCategories)
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3">  @T("Admin.ContentManagement.ModelTemplates").Text</label>
                        <div class="col-md-9">
                            <div class="input-group">
                                <span class="input-group-addon">
                                    @Html.CheckBoxFor(model => model.BatchCategory.OpenTemplateCheckBox)
                                </span>
                                @Html.DropDownList("TemplateId", Model.AvailableModelTemplates, "", new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.AvailableModelTemplates)
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3">
                            @T("Admin.ContentManagement.Recommend").Text
                        </label>
                        <div class="col-md-9">
                            <div class="input-group">
                                <span class="input-group-addon">
                                    @Html.CheckBoxFor(model => model.BatchCategory.OpenCheckBox)
                                </span>
                                <div class="btn-group" data-toggle="buttons">
                                    <label class="btn @(Model.BatchCategory.AllowComments ? "blue-hoki active" : "btn btn-default")">
                                        @Html.CheckBoxFor(model => model.BatchCategory.AllowComments, new { @class = "toggle" }) @Html.DisplayNameFor(model => model.BatchCategory.AllowComments)
                                    </label>
                                    <label class="btn @(Model.BatchCategory.IsTop ? "blue-madison active" : "btn btn-default")">
                                        @Html.CheckBoxFor(model => model.BatchCategory.IsTop, new { @class = "toggle" }) @Html.DisplayNameFor(model => model.BatchCategory.IsTop)
                                    </label>
                                    <label class="btn @(Model.BatchCategory.IsRed ? "green-meadow active" : "btn btn-default")">
                                        @Html.CheckBoxFor(model => model.BatchCategory.IsRed, new { @class = "toggle" }) @Html.DisplayNameFor(model => model.BatchCategory.IsRed)
                                    </label>
                                    <label class="btn @(Model.BatchCategory.IsHot ? "red-sunglo active" : "btn btn-default")">
                                        @Html.CheckBoxFor(model => model.BatchCategory.IsHot, new { @class = "toggle" }) @Html.DisplayNameFor(model => model.BatchCategory.IsHot)
                                    </label>
                                    <label class="btn @(Model.BatchCategory.IsSlide ? "yellow-crusta active" : "btn btn-default")">
                                        @Html.CheckBoxFor(model => model.BatchCategory.IsSlide, new { @class = "toggle" }) @Html.DisplayNameFor(model => model.BatchCategory.IsSlide)
                                    </label>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>

        }
    </text>)
    .FooterContent(@<text>
        <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">@T("Common.Close")</button>
        <button id="bulkEditSave" type="button" class="btn btn-primary">@T("Admin.Common.Save")</button>
    </text>)
   .Modal(true)
        .Visible(true)
        .Render();
}



